<template>
  <h2>1.职位描述</h2>
  <el-form :model="p" label-width="100px" style="width: 600px">
    <el-form-item label="职位描述:">
      <el-input v-model="p.post" type="textarea" />
    </el-form-item>
    <el-form-item label="职位名称:">
      <el-input v-model="p.title" />
    </el-form-item>
    <el-form-item label="关键词:">
      <el-input v-model="p.keyword" />
    </el-form-item>
  </el-form>

  <h2>2.职位要求</h2>
  <el-form :model="p" label-width="100px" style="width: 600px">
    <el-form-item label="经验和学历:">
      <el-col :span="11">
        <el-select v-model="p.experience" placeholder="请选择工作年限">
          <el-option label="不限" value="不限" />
          <el-option label="在校生" value="在校生" />
          <el-option label="应届生" value="应届生" />
          <el-option label="1年以内" value="1年以内" />
          <el-option label="1-3年" value="1-3年" />
          <el-option label="3-5年" value="3-5年" />
          <el-option label="5-10年" value="5-10年" />
          <el-option label="10年以上" value="10年以上" />
        </el-select>
      </el-col>
      <el-col :span="11">
        <el-select v-model="p.degree" placeholder="请选择学历">
          <el-option label="不限" value="不限" />
          <el-option label="初中及以下" value="初中及以下" />
          <el-option label="中专/中技" value="中专/中技" />
          <el-option label="高中" value="高中" />
          <el-option label="大专" value="大专" />
          <el-option label="本科" value="本科" />
          <el-option label="硕士" value="硕士" />
          <el-option label="博士" value="博士" />
        </el-select>
      </el-col>
    </el-form-item>
    <el-form-item label="薪资范围:">
        <el-select v-model="p.salary" placeholder="请选择薪资范围">
          <el-option label="不限" value="不限" />
          <el-option label="1-3K" value="1-3K" />
          <el-option label="3-5K" value="3-5K" />
          <el-option label="5-10K" value="5-10K" />
          <el-option label="10-20K" value="10-20K" />
          <el-option label="20-50K" value="20-50K" />
          <el-option label="50K以上" value="50K以上" />
        </el-select>
    </el-form-item>
    <el-form-item label="岗位要求:">
      <el-input v-model="p.jobRequire" />
    </el-form-item>
  </el-form>

  <el-row class="mb-4">
    <el-button size="large" @click="deletePosition()">删除</el-button>
    <el-button size="large" @click="router.push('/recruit/positionManage')">关闭</el-button>
    <el-button size="large" type="primary" @click="post()">{{p.id==null?'发布':'修改'}}职位</el-button>
  </el-row>
</template>

<script setup>
import {onMounted, reactive, ref} from 'vue';
import router from "@/router";
import qs from "qs";
import { ElMessage } from 'element-plus'
import axios from "axios";

const p = ref({post:"",title:"",keyword:"",experience:"",degree:"",salary:"",jobRequire:""});

const userId = ref({id:''});
let user=localStorage.user?JSON.parse(localStorage.user):null;
let Authorization=localStorage.user?JSON.parse(localStorage.Authorization):null;



const post = ()=>{
  userId.value.id = user;
  let data = qs.stringify(p.value);
  if (location.search.includes('id')){
    //修改职位
    let id = new URLSearchParams(location.search).get('id');
    //通过id请求内容详情数据
    axios.post('http://localhost:39281/v1/job/recruit/'+id+'/update-info',data,{
      headers:{Authorization}
    })
        .then((response)=>{
          if (response.data.state==20000){
            ElMessage({
              showClose: true,
              message: '修改成功!',
              type: 'success',
            })
            router.push('/recruit/positionManage/allPosition');
          }
        })
  }else {
//发布职位
  axios.post('http://localhost:39281/v1/job/recruit/'+userId.value.id+'/add-new',data,{
    headers:{Authorization}
  })
      .then((response)=>{
        if(response.data.state==20000){
          router.push('/recruit/positionManage/allPosition');
          ElMessage({
            showClose: true,
            message: '发布成功!',
            type: 'success',
          })
        }
      })
  }

}

const deletePosition = ()=>{
  let id = new URLSearchParams(location.search).get('id');
  if(confirm("您确认删除此内容吗?")){
    axios.post('http://localhost:39281/v1/job/recruit/'+id+'/delete','',{
      headers:{Authorization}
    }).then((response)=>{
          if(response.data.state==20000){
            ElMessage.success("删除成功!");
            router.push('/recruit/positionManage/allPosition');
          }
        })
  }
}

onMounted(()=>{


  //判断是否包含id
  if (location.search.includes('id')){
    let id = new URLSearchParams(location.search).get('id');
    //通过id请求内容详情数据
    axios.get('http://localhost:39281/v1/job/recruit/detail/'+id,{
      headers:{Authorization}
    })
        .then((response)=>{
          if (response.data.state==20000){
            p.value = response.data.data;
          }
        })
  }
})
</script>

<style scoped>

</style>
